package com.corewillsoft.fireflies.gameobjects.obstacles.trajectories;

import java.util.List;

/**
 * Class which implements calculations for rectangular trajectory of particular object
 */
public class RectangularTrajectory extends Trajectory {
	private static final int TRAJECTORY_PARAMETERS_COUNT = 4;

	/**
	 * X-coordinate of left top corner of rectangle of trajectory
	 */
	private float minX = 0f;

	/**
	 * Y-coordinate of left top corner of rectangle of trajectory
	 */
	private float minY = 0f;

	/**
	 * X-coordinate of right bottom corner of rectangle of trajectory
	 */
	private float maxX = 0f;

	/**
	 * Y-coordinate of right bottom corner of rectangle of trajectory
	 */
	private float maxY = 0f;

	/**
	 * Constructor of class
	 * @param parameters - list of float parameters, must contain 4 elements:
	 * <ul>
	 * <li>0 - X-coordinate of left top corner of rectangle of trajectory</li>
	 * <li>1 - Y-coordinate of left top corner of rectangle of trajectory</li>
	 * <li>2 - X-coordinate of right bottom corner of rectangle of trajectory</li>
	 * <li>3 - Y-coordinate of right bottom corner of rectangle of trajectory</li>*
	 * </ul>
	 */
	public RectangularTrajectory(List<Float> parameters) {
		super(parameters);

		int parametersCount = parameters.size();
		if (parametersCount != TRAJECTORY_PARAMETERS_COUNT) {
			return;
			//throw new RuntimeException("Wrong parameters count provided to RectangularTrajectory: expected " + TRAJECTORY_PARAMETERS_COUNT + ", but provided " + parametersCount);
		}

		minX = parameters.get(0);
		minY = parameters.get(1);
		maxX = parameters.get(2);
		maxY = parameters.get(3);
	}

	/**
	 * Calculates X- and Y- components of speed of object bound to this trajectory;
	 * object moves along rectangular trajectory determined by parameters passed to constructor of class,
	 * movement is always counter-clockwise;
	 * to get calculated speed components, one should call {@link com.corewillsoft.fireflies.gameobjects.obstacles.trajectories.Trajectory#getSpeedX()} and {@link com.corewillsoft.fireflies.gameobjects.obstacles.trajectories.Trajectory#getSpeedY()}
	 * after calling of this method
	 * @param x - current X-coordinate of the moving object
	 * @param y - current Y-coordinate of the object
	 * @param maxSpeed - maximal absolute value of speed of the moving object
	 */
	@Override
	public void calculateSpeed(float x, float y, float maxSpeed) {

		/*
		determine speed vector direction according to which edge of rectangle object is moving along
		(for now object moves always counter-clockwise)
		 */
		if (x <= minX) {
			if (y < maxY) {
				speedX = 0;                     //left edge
				speedY = maxSpeed;
			} else {							//left bottom corner
				speedX = maxSpeed;
				speedY = 0;
			}
		} else if (x < maxX) {
			if (y <= minY) {
				speedX = -maxSpeed;				//top edge
				speedY = 0;
			} else {
				speedX = maxSpeed;				//bottom edge
				speedY = 0;
			}
		} else {								//if (x >= maxX) - right edge
			if (y <= minY) {					//right top corner
				speedX = -maxSpeed;
				speedY = 0;
			} else {
				speedX = 0;						//right edge
				speedY = -maxSpeed;
			}
		}
	}
}
